<?php
	/**
	This file is part of PEL.
		PEL is free software: you can redistribute it and/or modify
		it under the terms of the GNU General Public License as published by
		the Free Software Foundation, either version 3 of the License, or
		(at your option) any later version.
	    
		PEL is distributed in the hope that it will be useful,
		but WITHOUT ANY WARRANTY; without even the implied warranty of
		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
		GNU General Public License for more details.
	    
		You should have received a copy of the GNU General Public License
		along with PEL.  If not, see <http://www.gnu.org/licenses/>.
	* Elgg pel: edit lang action
	*/

	// Make sure we're logged in (send us to the front page if not)
	gatekeeper();
  // make sure action is secure
  action_gatekeeper();

	// Get input data
  $guid = (int)get_input("post");
  $newlang = get_input("pellang");
  $access= get_input('access_id');
  // auto-eval 
  $ecouter = get_input('ecouter');
  $lire = get_input('lire');
  $conversation = get_input('conversation');
  $expression = get_input('exprimer');
  $ecrire = get_input('ecrire');
  //biographie
  $media= get_input('langmedia');
  //dossier
  if(is_array(get_input('champsSup'))){
    $champsSup = get_input('champsSup');
    foreach ($champsSup as $k => $v){
      $certificat[$k]= get_input('langcertificat_'.$k);
      $organisme[$k]= get_input('langorganisme_'.$k);
      $note[$k]=get_input('langnote_'.$k);
      $date[$k]=get_input('langdate_'.$k);
    } 
  }
  if(is_array(get_input('champsSupTravel'))){
    $champsSupTravel = get_input('champsSupTravel');
    foreach ($champsSupTravel as $k => $v){
      $travel[$k]= get_input('langtravel_'.$k);
      $traveldate[$k]= get_input('langtraveldate_'.$k);
    } 
  }
  if(is_array(get_input('champsSupTraining'))){
    $champsSupTraining= get_input('champsSupTraining');
    foreach ($champsSupTraining as $k => $v){
      $training[$k]= get_input('langtraining_'.$k);
      $trainingdate[$k]= get_input('langtrainingdate_'.$k);
    } 
  }

// Make sure we actually have permission to edit
  $lang = get_entity($guid);
  
  if ($lang->getSubtype() == "lang" && $lang->canEdit()){
    // Cache to the session
    $_SESSION['pellang'] = $newlang;
    // Make sure the title / description aren't blank
    if (empty($newlang)){
      register_error(elgg_echo("lang:blank"));
      forward("mod/pel/editlang.php?guid=".$guid);
  // Otherwise, save the pel language
    }
    else{
      // Get owning user
      $owner = get_entity($lang->getOwner());
      // Set its title and description appropriately

      $lang->description =$newlang;
      $lang->access_id = $access;
      if (!$lang->save()){
        register_error(elgg_echo("pel:error"));
        forward("mod/pel/editlang.php?guid=".$guid);
      }

      $lang->ecouter=$ecouter;
      $lang->lire=$lire;
      $lang->conversation=$conversation;
      $lang->expression=$expression;
      $lang->ecrire=$ecrire;
      $lang->travel=$travel;
      $lang->training=$training;
      $lang->traveldate=$traveldate;
      $lang->trainingdate=$trainingdate;
      $lang->media=$media;
      /*
      $file= new ElggFile();
      $file->owner_guid = $_SESSION['user']->getGUID();
      $file->container_guid = $lang->getGUID();
      $file->setFilename("lang/" . $_SESSION['user']->username . time() .$_FILES['file_upload']['name']);
      $file->setMimeType($_FILES['file_upload']['type']);
      $file->originalfilename = $_FILES['file_upload']['name'];
      $file->open("write");
      $file->write(get_uploaded_file('file_upload'));
      $file->close();
      $file->save();
      $lang->file=$file_upload;
      */
      $lang->certificat=$certificat;
      $lang->organisme=$organisme;
      $lang->note=$note;
      $lang->date=$date;
   // add_to_river('river/object/pel/update','update',$_SESSION['user']->guid,$lang->guid);

    // Success message
      system_message(elgg_echo("pel:saved"));
    // Remove the pel language cache
      unset($_SESSION['pellang']);
    // Forward to the main pel page
      forward("mod/pel/?username=" . $_SESSION['user']->username);
    }
  }
?>
